package cn.jn.lhm.netty.part08.chat.handler;

import cn.jn.lhm.netty.part08.chat.session.SessionFactory;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author lhm 2753914576@qq.com
 * @time 2023/11/1 - 21:08
 **/
@Slf4j
public class ServerQuitHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        String username = SessionFactory.getSession().getUsername(ctx.channel());
        SessionFactory.getSession().unbind(ctx.channel());
        log.info("客户端{}断开连接", username);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        String username = SessionFactory.getSession().getUsername(ctx.channel());
        // SessionFactory.getSession().unbind(ctx.channel());
        log.info("客户端[{}]异常退出,原因:[{}]", username, cause.getMessage());
    }
}
